#!/usr/bin/env python3
# -*- coding: utf-8 -*-
__author__ = 'z417'

import logging
import functools


def singleton(cls):
    """
    Singleton decorator
    :param cls: the object you want to set singleton
    :return:
    """
    instances = {}

    @functools.wraps(cls)
    def _wrapper(*args, **kwargs):
        if cls not in instances:
            instances[cls] = cls(*args, **kwargs)
        return instances[cls]
    return _wrapper


@singleton
class Log():

    def __init__(self, name='root'):
        """
        :param name:
        """
        self._logger = logging.getLogger(name)
        self._sh = logging.StreamHandler()
        self._fm = logging.Formatter(
            '[%(asctime)s %(module)s.%(funcName)s(%(lineno)d) %(levelname)s]: %(message)s')
        self._sh.setFormatter(self._fm)
        self._logger.addHandler(self._sh)
        self._logger.setLevel(logging.INFO)

    @property
    def get_logger(self):
        return self._logger


# Create a global logger object, named L
L = Log('webrunner').get_logger


if __name__ == "__main__":
    L.setLevel('WARN')
    L.info('this is info msg')
    L.debug('this is debug msg')
    L.error('this is error msg')
    L.critical('this is critical msg')
    L.warning('this is warning msg')
